package main

import (
	"io"
	"net/http"
	// "strings"
)

func main() {

	http.HandleFunc("/", Cookie)
	http.HandleFunc("/2", Cookie2)
	http.ListenAndServe(":80", nil)
}
func Cookie(w http.ResponseWriter, r *http.Request) {
	ck := &http.Cookie{Name: "mycookie", Value: "hello", Path: "/", Domain: "localhost", MaxAge: 120}
	http.SetCookie(w, ck)
	if ck2, err := r.Cookie("mycookie"); err != nil { //写入cookie存储到response中，需要从下次请求中读取cookie存入的cookie
		io.WriteString(w, err.Error())
	} else {
		io.WriteString(w, ck2.Value)
	}
}
func Cookie2(w http.ResponseWriter, r *http.Request) {
	ck := &http.Cookie{Name: "mycookie2", Value: "hello world!", Path: "/", Domain: "localhost", MaxAge: 120}
	// w.Header().Set("Set-Cookie", strings.Replace(ck.String(), " ", "%20", -1))
	w.Header().Set("Set-Cookie", ck.String())
	if ck2, err := r.Cookie("mycookie2"); err != nil {
		io.WriteString(w, err.Error())
	} else {
		io.WriteString(w, ck2.Value)
	}
}
